/**
 * 日期格式格式化
 * 将 yyyyMMdd格式的日期转为yyyy-MM-dd格式
 * @param {string}  date
 * @param {string}  pattern
 */
const dateFormat = (date = '', pattern = '') => {
	
	if (date == '' || date == null || date == undefined) return '-'

	let yearSeparator = '-'
	let monthSeparator = '-'
	let daySeparator = ''
	
	if (pattern.length === 3) {
		let patterns = pattern.split('')
		yearSeparator = patterns[0]
		monthSeparator = patterns[1]
		daySeparator = patterns[2] || ''
	}
	
	return `${date.substring(0, 4)}${yearSeparator}${date.substring(4, 6)}${monthSeparator}${date.substring(6, 8)}${daySeparator}`
}


/**
 * 日期格式格式化
 * 将 yyyy/MM/dd格式的日期转为yyyyMMdd
 */
const dateTransfer = (date = '', separator = '/') => {
	if (date === '') return new Date().toLocaleDateString().replaceAll('/', '')
	let arr = date.split(separator)
	return `${arr[0]}${concatZero(arr[1])}${concatZero(arr[2])}`
}

/**
 * 判断日期数组
 * 如果小于10则在前面添加0
 * eg: input 9 output 09
 * 	   input 10 output 10
 */
const concatZero = (n) => {
	let _n = Number(n)
	if (_n >= 10) return n
	return '0' + _n
}


export  {
	dateFormat, dateTransfer, concatZero
}